﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Input = Microsoft.Xna.Framework.Input;

/* 
 * 
 * 
 * 
 * 
 * 
 */




namespace MainApp
{
    class controller
    {
        /*a simple class for xbox controler input to winforms
         * values of keys are stored in bool's - objectName.keyName;
         * values of analog inputs are stored in short's - oblectName.analogStickName;
         * value of trigers have range 0 to 1000
         * value of sticks have range -1000 to 1000
         */
        private GamePadState gamePadState;
     //   private GamePadState previousState;
        private PlayerIndex playerIndex = PlayerIndex.One;//we will use just one gamepad
        
        //buttons
        public bool buttonA;
        public bool buttonB;
        public bool buttonX;
        public bool buttonY ;
        public bool buttonLeftShoulder;
        public bool buttonRightShoulder;
        public bool buttonStart;
        public bool buttonBack;
        public bool buttonLeftStick;
        public bool buttonRightStick;
        
        //dpad
        public bool buttonUp;
        public bool buttonDown;
        public bool buttonLeft;
        public bool buttonRight;

        //Sticks
        public short x1Position;
        public short y1Position;
        public short x2Position;
        public short y2Position;

        //trigers

        public short leftTriggerPosition;
        public short rightTriggerPosition;

        /*
         * This funtion are refreshing states of all button, sticks and other fun stuff on gamepad
         * in: nothing
         * out: it's void...
         * */
        public void refresh()
        {
            this.gamePadState = GamePad.GetState(this.playerIndex);
            //buttons
            buttonA = (this.gamePadState.Buttons.A == Input.ButtonState.Pressed);
            buttonB = (this.gamePadState.Buttons.B == Input.ButtonState.Pressed);
            buttonX = (this.gamePadState.Buttons.X == Input.ButtonState.Pressed);
            buttonY = (this.gamePadState.Buttons.Y == Input.ButtonState.Pressed);
            buttonLeftShoulder = (this.gamePadState.Buttons.LeftShoulder == Input.ButtonState.Pressed);
            buttonRightShoulder = (this.gamePadState.Buttons.RightShoulder == Input.ButtonState.Pressed);
            buttonStart = (this.gamePadState.Buttons.Start == Input.ButtonState.Pressed);
            buttonBack = (this.gamePadState.Buttons.Back == Input.ButtonState.Pressed);
            buttonLeftStick = (this.gamePadState.Buttons.LeftStick == Input.ButtonState.Pressed);
            buttonRightStick = (this.gamePadState.Buttons.RightStick == Input.ButtonState.Pressed);
        
            //dpad
            buttonUp = (this.gamePadState.DPad.Up == Input.ButtonState.Pressed);
            buttonDown = (this.gamePadState.DPad.Down == Input.ButtonState.Pressed);
            buttonLeft = (this.gamePadState.DPad.Left == Input.ButtonState.Pressed);
            buttonRight = (this.gamePadState.DPad.Right == Input.ButtonState.Pressed);

            //Sticks
            x1Position = (short)(this.gamePadState.ThumbSticks.Left.X * 1000);
            y1Position = (short)(this.gamePadState.ThumbSticks.Left.Y * 1000);
            x2Position = (short)(this.gamePadState.ThumbSticks.Right.X * 1000);
            y2Position = (short)(this.gamePadState.ThumbSticks.Right.Y * 1000);

            //trigers
            leftTriggerPosition = (short)(this.gamePadState.Triggers.Left * 1000);
            rightTriggerPosition = (short)(this.gamePadState.Triggers.Right * 1000);

        }

        //vibrtions
        /* This function controll vibrations of joypad
         * in: speed of left vibration engine (value from 0 to 1000) [this is a low freq motor], speed of right vibration engine (value from 0 to 1000) [this is a high freq motor]
         * out: void, but it's vibrate!!!
         * 
         */

        public void Vibrate(short left, short right)
        {
            float L = left;
            L = L / 1000;
            float R = right;
            R = R / 1000;
            GamePad.SetVibration(playerIndex, L, R);

        }


    }
}
